#include "commandFun.h"
#include "variableDefine.h"

/////////////////////////////////////////////////////////////////////////////////////////////
static char conn_ip[33] = {0};
static char conn_mac[33] = {0};
/////////////////////////////////////////////////////////////////////////////////////////////
void initfunc(SerialESP8266wifi& wifi) {
    int iplen = 0, maclen = 0;
    wifi.getIPMAC(conn_ip, 32, iplen, conn_mac, 32, maclen);
    conn_ip[iplen] = 0;
    conn_mac[maclen] = 0;
    wifi.showLog(String("ip: ") + conn_ip);
    wifi.showLog(String("mac: ") + conn_mac);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// 定义操作pin
extern pinState pinlist[];
extern int pin_size;

bool pin_on_off(SerialESP8266wifi& wifi, char chann, uint32_t number, bool on) {
    if (number <= 0 || number > pin_size) {
        return false;
    }
    auto& pin = pinlist[number - 1];
    if (pin.hight == on) {
        return true;
    }
    if (on == true) {
        digitalWrite(pin.pinID, HIGH);
        pin.hight = true;
    } else {
        digitalWrite(pin.pinID, LOW);
        pin.hight = false;
    }
    return true;
}

void pindeal(SerialESP8266wifi& wifi, char chann, const char* msg, int head,  bool on) {
    if (msg == nullptr || head <= 0) {
        wifi.send(chann, "ERROR");
        return;
    }
    auto len = strlen(msg);
    if (len < head) {
        wifi.send(chann, "command is ON(number[1-14])");
        return;
    }

    String points("all");

    if (len == head) {
        for (auto i = 0; i < pin_size; i++) {
            pin_on_off(wifi, chann, i + 1, on);
        }
    } else {
        String stmp(msg+head);
        char ch;
        uint32_t number = 0;
        int size = len - head;
        points = "";

        for (int index = 0; index < size; index ++) {
            ch = stmp[index];
            if (ch >= '0' && ch <= '9') {
                number = number * 10 + (uint32_t)(ch - '0');
                if (index < size - 1) {
                    continue;
                }
            }
            if (number <= 0) {
                continue;
            }
            if (ch == ' ' || ch == '\t' || ch == ',' || index == size - 1) {
                if(pin_on_off(wifi, chann, number, on) == true) {
                    auto& pin = pinlist[number - 1];
                    points = points + pin.pinID + ",";
                }
            }
            number = 0;
        }
    }
    
    if(on == true) {
        wifi.send(chann, String("on points: ") + points);
    } else {
        wifi.send(chann, String("off points: ") + points);
    }
}

void pinon(SerialESP8266wifi& wifi, char chann, const char* msg) {
    pindeal(wifi, chann, msg, 2, true);
}

void pinoff(SerialESP8266wifi& wifi, char chann, const char* msg) {
    pindeal(wifi, chann, msg, 3, false);
}

void pinstate(SerialESP8266wifi& wifi, char chann, const char* msg) {
    if (msg == nullptr) {
        wifi.send(chann, "ERROR");
        return;
    }
    auto len = strlen(msg);
    if (len <= 5) {
        wifi.send(chann, "command is STATE(number[1-14])");
        return;
    }
    int number = atoi(msg + 5);
    if (number <= 0 || number > pin_size) {
        wifi.send(chann, "number argument error");
        return;
    }
    auto& pin = pinlist[number - 1];
    if (pin.hight == true) {
        wifi.send(chann, String("pin") + pin.pinID + " on");
    } else {
        wifi.send(chann, String("pin") + pin.pinID + " off");
    }
}

void pinslist(SerialESP8266wifi& wifi, char chann, const char* msg) {
    String info;
    auto size = 0;
    for (auto i = 0; i < pin_size; i++) {
        auto& pin = pinlist[i];
        info = String("{\"index\":") + (i + 1) + ", \"id\":" + pin.pinID + ", \"state\":\"" + (pin.hight ? "on" : "off") + "\"}";
        if (i != pin_size - 1) {
            info += ",";
        }
        info += "\n";
        size += info.length();
    }
    if (wifi.startSend(chann, size + 4) == false) {
        wifi.send(chann, "error");
        return;
    }
    wifi.addSend(chann, "[\n");
    for (auto i = 0; i < pin_size; i++) {
        auto& pin = pinlist[i];
        info = String("{\"index\":") + (i + 1) + ", \"id\":" + pin.pinID + ", \"state\":\"" + (pin.hight ? "on" : "off") + "\"}";
        if (i != pin_size - 1) {
            info += ",";
        }
        info += "\n";
        wifi.addSend(chann, info);
    }
    wifi.endSend(chann, "]\n");
}

void restart(SerialESP8266wifi& wifi, char chann, const char* msg) {
    asm volatile("jmp 0");
}